package com.csj.mvvmtest.notification


import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import androidx.core.app.NotificationCompat
import com.csj.mvvm.bean.NotifyEntity
import com.csj.mvvm.utils.IntentExtras
import com.csj.mvvmtest.R
import java.util.*


class NotificationMr {

    companion object {
        private val NOTIFI_ID = 0x1001

        /**
         * 显示通知
         * 推送 "收到透传消息:" + data 时
         * NotifyEntity notifyEntity = new Gson().fromJson(data, NotifyEntity.class);
         * if (notifyEntity!=null) { NotifycationMr.showNotification(context, notifyEntity);}
         */
        fun showNotification(context: Context, notify: NotifyEntity?) {
            if (notify == null) {
                return
            }
            val notificationManager =
                context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            //跳转到通知的设置界面提醒用户手动打开 可运用于聊天消息
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            boolean enable = notificationManager.areNotificationsEnabled();//通知是否打开
//            NotificationChannel channel = notificationManager.getNotificationChannel("subscribe");
//            if (channel != null) {
//                if (!enable ||channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
//                    Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
//                    intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
//                    context.startActivity(intent);
//                    ToastUtil.setToast("请手动将通知打开");
//                }
//            }
//        }
            val calendar: Calendar = Calendar.getInstance()
            val intent = Intent(context, NotificationReceiver::class.java)
            intent.putExtra(IntentExtras.APS_DATA, notify)
            val code: Int =
                calendar.get(Calendar.HOUR) * 60 * 60 * 1000 + calendar.get(Calendar.MINUTE) * 60 * 1000 + calendar.get(
                    Calendar.SECOND
                ) * 1000 + calendar.get(Calendar.MILLISECOND)

            val contentIntent =
                PendingIntent.getBroadcast(context, code, intent, if (Build.VERSION.SDK_INT >= 31) PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_UPDATE_CURRENT)

            val channelId = if (notify.type == 1) "chat" else "subscribe"
            val defaultSoundUri: Uri =
                RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
            val lIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.app_icon_round)
            val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, channelId)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentTitle(notify.title)
                //.setContentText(notify.getContent())
                .setContentText("查看详情") //Html格式，目前写死
                .setSmallIcon(R.mipmap.ic_notify)
                .setLargeIcon(lIcon)
                //设置点击通知跳转页面后，通知消失
                .setAutoCancel(true)
                .setContentIntent(contentIntent)
                .setSound(defaultSoundUri)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channelName = if (notify.type == 1) "聊天消息" else "订阅消息"
                val channel =
                    NotificationChannel(
                        channelId,
                        channelName,
                        NotificationManager.IMPORTANCE_DEFAULT
                    )
                channel.enableLights(true) //显示桌面红点
                channel.lightColor = Color.RED
                notificationManager.createNotificationChannel(channel)
            }
            notificationManager.notify(NOTIFI_ID, builder.build())
        }

        /**
         * 取消通知
         *
         * @param context
         */
        fun cancelNotify(context: Context) {
            val notificationManager = context
                .getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.cancel(NOTIFI_ID)
        }
    }

}